
package cn.com.starbucks.common.bean;

import cn.com.starbucks.common.constant.CommonConstants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import java.io.Serializable;

 /**
 * Description: 响应信息主体
 *
 * @author tecsmile@outlook.com
 * @version 1.0
 * @date 2021/4/20 9:42 上午
 **/
@NoArgsConstructor
@Accessors(chain = true)
@Data
@ApiModel(value = "rest请求方式响应信息结构体")
public class RestResponse<T> implements Serializable {

    private static final long serialVersionUID = -6037246128710553283L;

    @ApiModelProperty(value = "返回标记：成功标记=0，失败标记=1")
    private int code;

    @ApiModelProperty(value = "返回信息")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    public static <T> RestResponse<T> success() {
        return restResponse(null, CommonConstants.SUCCESS, "success");
    }

    public static <T> RestResponse<T> success(T data) {
        return restResponse(data, CommonConstants.SUCCESS, "success");
    }

    public static <T> RestResponse<T> success(T data, String msg) {
        return restResponse(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> RestResponse<T> failed() {
        return restResponse(null, CommonConstants.FAIL, "failed");
    }

    public static <T> RestResponse<T> failed(String msg) {
        return restResponse(null, CommonConstants.FAIL, msg);
    }

    public static <T> RestResponse<T> failed(T data) {
        return restResponse(data, CommonConstants.FAIL, "failed");
    }

    public static <T> RestResponse<T> failed(T data, String msg) {
        return restResponse(data, CommonConstants.FAIL, msg);
    }

    private static <T> RestResponse<T> restResponse(T data, int code, String msg) {
        RestResponse<T> result = new RestResponse<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
}

